package com.eeepay.eeepay_v2.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Filter;
import android.widget.Filterable;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.model.BankInfo;
import com.eeepay.v2_library.adapter.ABBaseAdapter;
import com.eeepay.v2_library.adapter.ABViewHolder;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * Created by Ching on 2016/8/22.
 */
public class BankAdapter extends ABBaseAdapter<BankInfo> implements Filterable {

    List<BankInfo> alllist = new ArrayList<BankInfo>();

    /**
     * @param context
     */
    public BankAdapter(Context context) {
        super(context);
    }

    public void setAllList(List<BankInfo> list) {
        alllist.addAll(list);
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_select_branch_listview;
    }

    @Override
    public void convert(ABViewHolder holder, BankInfo model) {
        holder.setText(R.id.tv_select_branch, model.getBank_name());
    }

    @Override
    public Filter getFilter() {

        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                List<BankInfo> c_list = new ArrayList<BankInfo>();

                if (TextUtils.isEmpty(constraint)) {
                    c_list.addAll(alllist);
                } else {
                    for (Iterator<BankInfo> iterator = alllist.iterator(); iterator.hasNext(); ) {
                        BankInfo p = iterator.next();
                        String filterType = p.getBank_name();
                        if (filterType.contains(constraint.toString())) {
                            c_list.add(p);
                            continue;
                        }
                    }
                }
                filterResults.values = c_list;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                setList((List<BankInfo>) results.values);
                notifyDataSetChanged();
            }
        };
        return filter;
    }
}
